// 地址本插件
layui.define(['ksSearch'], function(exports) {
  var layer = layui.layer
    , $ = layui.jquery
    , search = layui.ksSearch;

  var tableConfig = {
    id: 'addrList',
    toolbar: '',
    height: 480,
    elem: '#addrTable',
  };

  function getConfig(_config) {
    return $.extend(true, _config, tableConfig);
  }

  var address = {
    getConfig: getConfig,
    config: {
      dept: getConfig({
        url: API.DEPT.BASE,
        cols: [[
          {field: 'id', hide: true},
          {field: 'name', title: '名称'},
          {field: 'no', title: '编号', sort: true},
          {field: 'weight', title: '权重'},
          {field: 'parent', title: '上级组织'}
        ]],
        tableFilter: {
          filters: [
            {field: 'name', name: 'search.name_like', type: 'input'},
            {field: 'no', name: 'search.no_eq', type: 'input'},
          ]
        },
        where: {
          'search.isDelete_eq': false,
          'search.type_eq': 'dept'
        }
      }),
      person: getConfig({
        url: API.PERSON.BASE,
        cols: [[
          {field: 'id', hide: true},
          {field: 'name', title: '名称', width: 200},
          {field: 'no', title: '员工号', sort: true, width: 120},
          {field: 'parentName', title: '部门', width: 200},
          {field: 'mobile', title: '联系电话', width: 150},
          {field: 'email', title: '邮箱', width: 150},
        ]],
        tableFilter: {
          filters: [
            {field: 'name', name: 'search.name_like', type: 'input'},
            {field: 'no', name: 'search.no_eq', type: 'input'},
            {field: 'mobile', name: 'search.mobile_eq', type: 'input'},
            {field: 'email', name: 'search.email_start', type: 'input'},
          ]
        },
        where: {
          'search.isDelete_eq': false,
          'projection': 'userInfo'
        }
      })
    },
    set: function(options) {
      var that = this;
      $.extend(true, that.config, options);
      return that;
    },
    init: function() {
      var _config = this.config
        , addrList = $('.ok-address');

      layui.each(addrList, function(index, item) {
        var addr = $(item)
          , type = addr.data('addr')
          , select = addr.data('select') || 'radio'
          , target = addr.data('target')
          , tableConfig = _config[type];
        addr.unbind('click')
          .click(function() {
            layer.open({
              id: 'addressWindow_' + type,
              type: 1,
              title: '地址簿',
              content: '<table id="addrTable" class="layui-hide" lay-filter="listTable"></table>',
              area: ['90%', '90%'],
              btn: ['确定', '清空'],
              success: function(layero, index) {
                if(tableConfig.cols[0][0].type !== select) {
                  tableConfig.cols[0].splice(0, 0, {type: select, fixed: true})
                }
                search.set(tableConfig).init();
              },
              yes: function(index, layero) {
                var data = search.getSelect();
                if(typeof tableConfig['callback'] === 'function') {
                  tableConfig['callback'](data);
                } else if(target) {
                  $('[name="' + target + 'Id"]').val(getListStr(data, 'id'));
                  $('[name="' + target + 'Name"]').val(getListStr(data, 'name'));
                }
                layer.close(index); //如果设定了yes回调，需进行手工关闭
              },
              btn2: function(index, layero) {
                $('[name="' + target + 'Id"]').val('');
                $('[name="' + target + 'Name"]').val('');
                layer.close(index); //如果设定了yes回调，需进行手工关闭
              }
            })
          });
      });
    }
  };

  function getListStr(list, field) {
    if(list.length > 1) {
      var rtn = [];
      layui.each(list, function(index, item) {
        rtn.push(item[field]);
      });
      return rtn.join(',');
    } else {
      return list[0][field];
    }
  }

  exports('ksAddress', address);
});
